/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package leastsquaresapproximation;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 *
 * @author PavelsKletnojs
 */
public class ReadFile {
    public double[][] matrixAsArray;
    public ArrayList<ArrayList<Double>> matrixAsList;

    public ReadFile() throws Exception {
	this.matrixAsList = readMatrixFromFile();
	this.matrixAsArray = generate2dArrayFromList();
    }

    public static double[][] getStaticMatrix(){
	double[][] matrix;
	
	matrix = new double[][]
	    {{1, 2, 3, 4, 30},
	    {-1, 2, -3, 4, 10},
	    {0, 1, -1, 1, 3},
	    {1, 1, 1, 1, 10}};
//*/
/*
	matrix = new double[][]
	    {{1, 1, 1, 1, 5},
	    {1, 1, 1, 1, 5},
	    {1, 1, 1, 1, 5},
	    {1, 1, 1, 1, 5}};
//*/	
	
/*	
	matrix = new double[][]
	    {{0, 2, 3, 2, 0},
	    {1, 3, 5, 1, 2},
	    {1, 2, 4, 2, 1},
	    {3, 1, 3, 2, 6}};
//*/
/*
	matrix = new double[][]
	    {{1, 2, 3, 3},
	    {3, 5, 7, 0},
	    {1, 3, 4, 1}};
//*/
	return matrix;
    }
    
    private double[][] generate2dArrayFromList(){
	double M[][] = new double[matrixAsList.size()][matrixAsList.get(0).size()];
	for(int i = 0; i<matrixAsList.size(); i++){
	    for(int j = 0; j<matrixAsList.size()+1; j++){
		M[i][j] = matrixAsList.get(i).get(j);
	    }
	}
	return M;
    }

    private ArrayList<ArrayList<Double>> readMatrixFromFile() throws Exception{
	// Create Array of strings from file.
	ArrayList<String> matrixStrings = new ArrayList<String>();
	try{
	    // Open the file that is the first 
	    // command line parameter
	    FileInputStream fstream = new FileInputStream("./matrix.txt");
	    // Get the object of DataInputStream
	    DataInputStream in = new DataInputStream(fstream);
	    BufferedReader br = new BufferedReader(new InputStreamReader(in));
	    String strLine;
	    //Read File Line By Line
	    while ((strLine = br.readLine()) != null)   {
		// Print the content on the console
		matrixStrings.add(strLine);
	    }
	    //Close the input stream
	    in.close();
	}
	catch (Exception e){//Catch exception if any
	    throw e;
	}
	
	// Parse array of strings to Double matrix 
	StringTokenizer st;
	ArrayList<ArrayList<Double>> matrix = new ArrayList<ArrayList<Double>>();	
	for(int i = 0; i<matrixStrings.size(); i++){
	    matrix.add(new ArrayList<Double>());
	    st = new StringTokenizer(matrixStrings.get(i));
	    for(int j = 0; st.hasMoreTokens(); j++){
		matrix.get(i).add(Double.parseDouble(st.nextToken()));
	    }	
	}
	checkMatrixLegal(matrix);
	
	return matrix;
    }
    
    private void checkMatrixLegal(ArrayList<ArrayList<Double>> matrix)
		    throws Exception{
	// Check if matrix is legal
	if(matrix.isEmpty() || matrix.size() != 2)
	    throw new Exception("Matrix is not legal!");

	if(matrix.get(0).size() != matrix.get(1).size())
	    throw new Exception("Matrix is not legal!");
    }
}
